#include"singleNode.h"

Node* createNode(int x)
{
	Node* newNode = (Node*)malloc(sizeof(Node));
	if (newNode == NULL)
	{
		printf("malloc failed\n");
		exit(-1);
	}
	newNode->value = x;
	newNode->next = NULL;
	return newNode;
}

void printLink(Node* p)
{
	assert(p);
	while (p != NULL)
	{
		printf("%d ", p->value);
		p = p->next;
	}
	printf("NULL\n");
}

Node* createLinkFront()
{
	Node* head = NULL;
	int value = 0;
	scanf("%d", &value);
	while (value!=0)
	{
		Node* newNode = createNode(value);
		newNode->next = head;
		head = newNode;
		scanf("%d", &value);
	}
	return head;
}

Node* createLinkBack()
{
	Node* head = NULL,*tail = NULL;
	int value = 0;
	scanf("%d", &value);
	while (value != 0)
	{
		Node* newNode = createNode(value);
		if (head == NULL)
			tail = head = newNode;
		else
			tail = tail->next = newNode;
		scanf("%d", &value);
	}
	return head;
}


Node* linkInverseFront(Node* head)
{
	assert(head);
	Node* cur = head->next, * curNext = NULL;
	head->next = NULL;
	while (cur!=NULL)
	{
		curNext = cur->next;
		cur->next = head;
		head = cur;
		cur = curNext;
	}
	return head;
}

Node* linkInverseBack(Node* head)
{
	assert(head);
	Node* cur = head, * curNext = head->next,*newHead = head;
	while (newHead->next != NULL)
		newHead = newHead->next;
	while (head->next != NULL)
	{
		cur = head; curNext = cur->next;
		while (curNext->next != NULL)
		{
			cur = cur->next;
			curNext = curNext->next;
		}
		cur->next = NULL;
		curNext->next = cur;
	}
	return newHead;
}